package com.jxzyk.model;

import org.json.JSONException;

import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.core.ui.activity.BaseActivity;
import com.core.util.SystemTool;
import com.core.util.T;
import com.core.util.UiTools;
import com.jxzyk.R;
import com.jxzyk.adapter.CourseAdapter;
import com.jxzyk.config.API;
import com.jxzyk.entity.Course;
import com.jxzyk.entity.User;
import com.jxzyk.http.Query;
import com.jxzyk.http.BaseRequest.OnDialogListener;
import com.jxzyk.http.req.CourseRequest;
import com.jxzyk.http.req.LoginRequest;
import com.jxzyk.model.users.LoginActivity;
import com.jxzyk.util.UserManger;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.act_loading)
public class LoadingActivity extends BaseActivity {
	private UserManger manger;
	@ViewInject(R.id.tv_load_versionName)
	TextView tv_versionName;

	@Override
	public void initData(Bundle bundle) {
		manger = new UserManger(getBaseContext());
	}

	@Override
	public void initUi() {
		tv_versionName.setText("版本号："
				+ SystemTool.getVersionName(getBaseContext()));
		// loadList("实训");
		new Handler().postDelayed(new Runnable() {

			@Override
			public void run() {
				try {
					boolean savePwd = manger.getSave();
					boolean saveAuto = manger.getAuto();
					if (saveAuto) {
						login();
					} else {
						showActivity(LoginActivity.class);
						finish();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}, 3000);
	}

	private void loadList(String keywords) {
		CourseRequest request = new CourseRequest(this, API.Course.getCourses);
		Query query = new Query();
		query.setKeywords(keywords);
		request.setQuery(query);
		request.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				try {
					JSONArray array = json.getJSONArray("data");
					if (array.size() > 0) {
						for (int i = 0; i < array.size(); i++) {
							Course course = (Course) new Course()
									.instance(array.getJSONObject(i));
						}
					}
					// Call onRefreshComplete when the list has been refreshed.
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				// Call onRefreshComplete when the list has been refreshed.
			}
		});
		request.show();
	}

	private void login() throws Exception {
		String userName = manger.getUserName();
		User user = manger.getUser();
		if (user == null) {
			showActivity(LoginActivity.class);
			finish();
			return;
		}
		String pwd = user.getUser_pwd();
		LoginRequest dialog = new LoginRequest(LoadingActivity.this);
		dialog.setParams(userName, pwd);
		dialog.setOnDialogListener(new OnDialogListener() {

			@Override
			public void onSuccess(JSONObject json) {
				try {
					manger.saveUser(json.toJSONString());
					showActivity(MainActivity.class);
					finish();
				} catch (JSONException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

			@Override
			public void onFailure(String msg) {
				// T.toast("会话过期，请重新登录");
				showActivity(LoginActivity.class);
				finish();
			}
		});
		dialog.start();
	}
}
